package com.bnyy.medicalHousekeeper.moudle.message.adapter;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;

import com.blankj.utilcode.util.SizeUtils;
import com.blankj.utilcode.util.TimeUtils;
import com.bnyy.common.adapter.BaseNormalListAdapter;
import com.bnyy.medicalHousekeeper.GlideHelper;
import com.bnyy.medicalHousekeeper.R;
import com.bnyy.message.bean.GroupChatActivity;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.function.Consumer;

/**
 * Author : 关炳鹏
 * Description :
 * Date : 2023/1/5
 **/
public class GroupChatActivityAdapter extends BaseNormalListAdapter<GroupChatActivity, GroupChatActivityAdapter.ViewHolder> {

    public GroupChatActivityAdapter(Context context, Consumer<GroupChatActivity> consumer) {
        super(context, consumer);
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ViewHolder(inflater.inflate(R.layout.item_group_chat_activity, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        super.onBindViewHolder(holder, position);

        GroupChatActivity groupChatActivity = getData(position);
        GlideHelper.setHeader(mContext, groupChatActivity.getUser_img(), holder.iv);
        holder.tvName.setText(groupChatActivity.getUsername());
        holder.tvSubject.setText(groupChatActivity.getSubject());
        holder.tvCount.setText(groupChatActivity.getNumber() + "人参与");
        try {
            Date date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(groupChatActivity.getDatetime());

            if (date != null) {
                String time;
                if (TimeUtils.isToday(date)) {
                    time = new SimpleDateFormat("HH:mm").format(date);
                } else {
                    Calendar calendar = Calendar.getInstance();
                    calendar.setTimeInMillis(System.currentTimeMillis());
                    int x = calendar.get(Calendar.DAY_OF_YEAR);

                    calendar.setTimeInMillis(date.getTime());
                    int y = calendar.get(Calendar.DAY_OF_YEAR);

                    if (x - y == 1) {
                        time = "昨天 " + new SimpleDateFormat("HH:mm").format(date);
                    } else {
                        time = new SimpleDateFormat("yyyy-MM-dd HH:mm").format(date);
                    }
                }
                holder.tvTime.setText(time + "发布");
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }

        GradientDrawable bg = (GradientDrawable) holder.tvIsJoined.getBackground();
        if (bg == null) {
            bg = new GradientDrawable();
            bg.setCornerRadius(SizeUtils.dp2px(5));
            holder.tvIsJoined.setBackground(bg);
        }
        bg.setColor(Color.parseColor(groupChatActivity.isJoined() ? "#F1F1F1" : "#EAF8EF"));
        holder.tvIsJoined.setText(groupChatActivity.isJoined() ? "已参加" : "未参加");
        holder.tvIsJoined.setTextColor(ContextCompat.getColor(mContext, groupChatActivity.isJoined() ? R.color.gray_light : R.color.green_theme));
    }

    public static class ViewHolder extends BaseNormalListAdapter.ViewHolder {

        ImageView iv;
        TextView tvName;
        TextView tvTime;
        TextView tvSubject;
        TextView tvIsJoined;
        TextView tvCount;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);

            iv = itemView.findViewById(R.id.iv);
            tvName = itemView.findViewById(R.id.tv_name);
            tvTime = itemView.findViewById(R.id.tv_time);
            tvSubject = itemView.findViewById(R.id.tv_subject);
            tvIsJoined = itemView.findViewById(R.id.tv_is_joined);
            tvCount = itemView.findViewById(R.id.tv_count);
        }
    }
}
